package club.orqlog.schedule.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzMain {

    public static Scheduler createScheduler() throws SchedulerException {
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();
        return scheduler;
    }

    public static JobDetail createJob() {
        JobBuilder jobBuilder = JobBuilder.newJob(DemoJob.class);
        jobBuilder.withIdentity("jobName", "DemoJob");
        JobDetail jobDetail = jobBuilder.build();
        return jobDetail;
    }

    public static Trigger createTriger() {
        // 创建时间触发器, 按日历调度
        CronTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("triggerName", "myTrigger")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?"))
                .build();

        // 创建时间触发器, 按简单间隔调度
        SimpleTrigger trigger1 = TriggerBuilder.newTrigger()
                .withIdentity("triggerName", "mySimpleTrigger")
                .startNow()
                .withSchedule(SimpleScheduleBuilder
                                .simpleSchedule()
                                .withIntervalInSeconds(3)
                                .repeatForever())
                .build();

        return trigger;
    }

    public static void main(String[] args) throws SchedulerException {
        Scheduler scheduler = QuartzMain.createScheduler();
        JobDetail job = QuartzMain.createJob();
        Trigger trigger = QuartzMain.createTriger();
        scheduler.scheduleJob(job, trigger);
        scheduler.start();
    }

}
